<template>
  <el-card shadow="hover" style="width: 350px">
    <template #header>
      <div class="option-header">
        <div class="option-header-content"><span>三层阶梯</span></div>
        <div v-if="props.calcSuccess=='true'" class="option-header-content"><span style="color: #22bb98 ">计算成功</span></div>
        <!--        <div v-if="props.calcSuccess=='false'" class="option-header-content"><span style="color: #e53c3c">计算失败</span></div>-->
        <div v-if="props.calcSuccess=='false'" class="option-header-content">
          <el-tooltip  effect="light"  >
            <template #content> <div style="width: 280px;">
              <span style="color: #e53c3c;">{{props.errorMsg}}</span>
            </div> </template>
            <span style="color: #e53c3c">计算失败</span>
          </el-tooltip>
        </div>
        <div>
          <el-button style="background: transparent;" :disabled="cpoyWait" v-if="props.useType=='calc'" class="delete" @click="copy" title="复制">
            <el-icon>
              <copy-document/>
            </el-icon>
          </el-button>
          <el-button style="background: transparent;" class="delete" @click="cancel" title="关闭">
            <el-icon>
              <circle-close/>
            </el-icon>
          </el-button>
        </div>
      </div>
    </template>

    <el-form ref="formRef" :disabled="form.data.readOnly" :model="form.data" label-width="auto" size="small"
             :rules="rules">
      <div class="optionst01">
        <el-form-item label="交易方向" prop="payDirection">
          <el-radio-group v-model="form.data.payDirection" size="small" :fill="getColor()">
            <el-radio-button label="1">买入</el-radio-button>
            <el-radio-button label="-1">卖出</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标的" label-width="auto" prop="instrumentId">
          <Underlyer ref="underlyerIdRef" :filterDate="true" v-model="form.data.instrumentId" @change="onBlurInstrumentId" :underlyerId="form.data.instrumentId"/>
        </el-form-item>
        <!--合约乘数-->
        <!--      todo-->
        <!--涨跌类型-->
        <el-form-item label="涨跌类型" label-width="auto" prop="optionType">
          <el-radio-group v-model="form.data.optionType" size="small">
            <el-radio label="Call">看涨</el-radio>
            <el-radio label="Put">看跌</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="起始日" label-width="auto" prop="effectiveDate">
          <el-date-picker value-format="YYYY-MM-DD" v-model="form.data.effectiveDate" @blur="onBlurEffectiveDate" type="date" placeholder="">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="期限" label-width="auto" prop="term">
          <Numberinput maxlength="4" v-model="form.data.term" append="D"/>
        </el-form-item>

        <el-form-item label-width="auto" label="观察日" prop="terminationDate">
          <el-date-picker value-format="YYYY-MM-DD" v-model="form.data.terminationDate" type="date" placeholder=""/>
        </el-form-item>

        <el-form-item label="到期支付偏移" label-width="auto" prop="paymentDaysOffset">
          <Numberinput maxlength="4" v-model="form.data.paymentDaysOffset" append="D"/>

        </el-form-item>

        <el-form-item label-width="auto" label="到期支付日" prop="terminationDate">
          <el-date-picker value-format="YYYY-MM-DD" v-model="form.data.terminationPaymentDate"
                          type="date"
                          placeholder=""/>
        </el-form-item>

        <el-form-item label="名义本金" prop="notional">
          <InputAmount v-model:value="form.data.notional" maxlength="20" append="元" precision="2"></InputAmount>
        </el-form-item>


        <el-form-item label="期初价格" label-width="auto" prop="spotPrice" >
          <el-col :span="8">
            <el-checkbox name="status" false-label="0" true-label="1" v-model="form.data.priceOnWait">待定</el-checkbox>
          </el-col>
          <el-col :span="16">
            <InputAmount  @change="() => {proxy.$refs.PricingParamRef?proxy.$refs.PricingParamRef.calcRateByCode():''}" v-model:value="form.data.spotPrice" maxlength="20" :disabled="form.data.priceOnWait=='1'"
                          @blur="spotPriceBlur"
                         append="元" precision="2"/>
          </el-col>
        </el-form-item>

        <el-form-item label="交易数量" prop="optionEntitlement">
          <Numberinput v-model="form.data.optionEntitlement" precision="2" maxlength="20" :disabled="form.data.priceOnWait=='1'"  @change="optionEntitlementChange"/>
        </el-form-item>
        <el-form-item v-show="false" label="参与率" prop="participationRate">
          <Numberinput v-model="form.data.participationRate" precision="2" maxlength="20" append="%"/>
        </el-form-item>
      </div>
      <!---------------------------------------------- 分割线---------------------------------------------------------->
      <div class="optionst02">
        <el-form-item label="高行权价比例" prop='strikePercentage'>
          <Numberinput @blur="()=>{Dialog.type='';linkPercentage(form,form.data.strikePercentage,form.data.spotPrice,'data.strike')}" v-model="form.data.strikePercentage"
                       maxlength="18" precision="2"
                       append="%" :input-style="Dialog.type=='strikePercentage'?'color: red;':''"/>
        </el-form-item>
        <el-form-item label="高行权价" prop='strike'>
          <InputAmount v-model:value="form.data.strike" maxlength="20"
                       :input-style="Dialog.type=='strikePercentage'?'color: red;':''" 
                       @blur="()=>{Dialog.type='';linkPrice(form,form.data.strike,form.data.spotPrice,'data.strikePercentage')}" append="元" precision="2"/>
        </el-form-item>

        <el-form-item label="低行权价比例" prop='lowStrikePercentage'>
          <Numberinput @blur="()=>{Dialog.type='';linkPercentage(form,form.data.lowStrikePercentage,form.data.spotPrice,'data.lowStrike')}" v-model="form.data.lowStrikePercentage"
                       maxlength="18" precision="2"
                       append="%" :input-style="Dialog.type=='lowStrikePercentage'?'color: red;':''"/>
        </el-form-item>
        <el-form-item label="低行权价" prop='lowStrike'>
          <InputAmount v-model:value="form.data.lowStrike" maxlength="20"
                       :input-style="Dialog.type=='lowStrikePercentage'?'color: red;':''"
                       @blur="()=>{Dialog.type='';linkPrice(form,form.data.lowStrike,form.data.spotPrice,'data.lowStrikePercentage')}" append="元" precision="2"/>
        </el-form-item>

        <el-form-item label="高档收益率" prop='paymentAmountPecentage'>
          <el-col :span="8">
            <el-checkbox v-model="form.data.moneynessOption" name="status" false-label="0" true-label="1">年化
            </el-checkbox>
          </el-col>
          <el-col :span="16">
            <Numberinput v-model="form.data.paymentAmountPecentage" :max="1000000000" :min="0" append="%"
                         precision="2" @blur="Dialog.type='';onPaymentAmountPecentageChange()"
                         :input-style="Dialog.type=='paymentAmountPecentage'?'color: red;':''"/>
          </el-col>
        </el-form-item>
        <el-form-item label="高档收益金额" prop='paymentAmount'>
          <InputAmount v-model:value="form.data.paymentAmount" @blur="Dialog.type='';onPaymentAmountChange()"
                       :input-style="Dialog.type=='paymentAmountPecentage'?'color: red;':''"
                       :min="0" precision="2"/>
        </el-form-item>

        <el-form-item label="中档收益率" prop='midPaymentAmountPecentage'>
          <el-col :span="8">
            <el-checkbox v-model="form.data.midMoneynessOption" name="status" false-label="0" true-label="1">年化
            </el-checkbox>
          </el-col>
          <el-col :span="16">
            <Numberinput v-model="form.data.midPaymentAmountPecentage" :max="1000000000" :min="0" append="%"
                         precision="2" @blur="Dialog.type='';onMidPaymentAmountPecentageChange()"
                         :input-style="Dialog.type=='midPaymentAmountPecentage'?'color: red;':''"/>
          </el-col>
        </el-form-item>
        <el-form-item label="中档收益金额" prop='midPaymentAmount'>
          <InputAmount v-model:value="form.data.midPaymentAmount" @blur="Dialog.type='';onMidPaymentAmountChange()"
                       :input-style="Dialog.type=='midPaymentAmountPecentage'?'color: red;':''"
                       :min="0" precision="2"/>
        </el-form-item>

        <el-form-item label="低档收益率">
          <el-col :span="8">
            <el-checkbox v-model="form.data.lowMoneynessOption" name="status" false-label="0" true-label="1">年化
            </el-checkbox>
          </el-col>
          <el-col :span="16">
            <Numberinput v-model="form.data.lowPaymentAmountPecentage" :max="1000000000" :min="0" append="%"
                         :input-style="Dialog.type=='lowPaymentAmountPecentage'?'color: red;':''"
                         precision="2" @blur="Dialog.type='';onLowPaymentAmountPecentageChange()"/>
          </el-col>
        </el-form-item>
        <el-form-item label="低档收益金额" prop='lowPaymentAmount'>

          <InputAmount v-model:value="form.data.lowPaymentAmount" @blur="Dialog.type='';onLowPaymentAmountChange()"
                       :input-style="Dialog.type=='lowPaymentAmountPecentage'?'color: red;':''" :min="0"
                       precision="2"/>
        </el-form-item>

        <el-form-item v-show="false" label="计息天数">
          <Numberinput v-model="form.data.term" append="D"/>
        </el-form-item>

        <el-form-item v-show="form.data.moneynessOption =='1' || form.data.lowMoneynessOption =='1'|| form.data.midMoneynessOption =='1'" label="计息基准">
          <dictCombox v-model="form.data.daycounter" prop='disDayCount'
                      dictType="dayCounter"
                      :style="{width: '100%'}"></dictCombox>
        </el-form-item>


        <el-form-item class="operation">
          <el-button type="primary" @click="calc" v-show="props.useType=='calc'||props.useType=='scene'"
                     :disabled="form.data.priceOnWait=='1'">计算
          </el-button>
          <el-button type="primary" @click="calcInvert" v-show="props.useType=='calc'||props.useType=='scene'"
                     :disabled="form.data.priceOnWait=='1'">反算
          </el-button>
          <el-button @click="save2CalcLog" v-show="props.useType=='calc'" :disabled="form.data.priceOnWait=='1'">存为草稿</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="optionst03">
      <PricingParam ref="PricingParamRef" :formData="form.data"  :valueDate="props.valueDate"  :exerciseType="form.data.exerciseType" :index="props.index"
                    label-width="auto" :filterList="form.data.filterList" :disabled="form.data.readOnly"
                    :productType="productType" size="small"
                    :calcPricingParam="props.calcPricingParam"></PricingParam>
    </div>
    <div v-show="props.useType!='scene'">
      <singleResult ref="resultCompRef" :result="form.result" :index="props.index"  :form="form"
        :calcInvert="props.useType=='calc'"></singleResult>
    </div>
  </el-card>
  <el-dialog
      v-model="calcInvertVisible"
      title="反算结果"
      width="20%"
      destroy-on-close
      center
  >
    <el-form ref="DialogForm" disabled :model="Dialog.data" label-width="auto" size="small">
      <el-form-item v-if="Dialog.showMoneynessOption" :label="Dialog.data.labelPercentage">
        <el-col :span="8">
          <el-checkbox v-model="Dialog.data.moneynessOption" name="status" false-label="0" true-label="1">年化
          </el-checkbox>
        </el-col>
        <el-col :span="16">
          <Numberinput v-model="Dialog.data.valuePercentage" maxlength="18" precision="2" append="%"/>
        </el-col>
      </el-form-item>
      <el-form-item v-if="!Dialog.showMoneynessOption" :label="Dialog.data.labelPercentage">
        <Numberinput v-model="Dialog.data.valuePercentage" maxlength="18" precision="2" append="%"/>
      </el-form-item>
      <el-form-item :label="Dialog.data.labelAmount">
        <Numberinput v-model="Dialog.data.valueAmount" maxlength="18" precision="2" append="元"/>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import {
  getCurrentInstance,
  nextTick,
  onMounted,
  onUpdated,
  provide,
  reactive,
  ref,
  toRaw,
  toRefs,
  watch
} from 'vue'
import Underlyer from '@/xquant/base/underlyer'
import Numberinput from '@/xquant/base/number'
import InputAmount from '@/xquant/base/InputAmount'
import dictCombox from '@/xquant/base/dictCombox'
import PricingParam from '@/views/calc/otcOtion/pricingparam/index'
import singleResult from '@/views/calc/otcOtion/result/single'
import {getTradeDay} from '@/utils/calc'
import {formartFormP4, formartTableP2, unFormartNumber, formartFormP2, numberPrecision} from '@/utils/numberUtil'
import {
  isVoid,
  isNotVoid,
  isVoidArg,
  isNotVoidArg,
  calcAnnualizedByTerminationPaymentDate,
  isEquativesABC,
  isEquativesAnnualizedABC,
  isEqualNum,
  mulArg,
  round,
} from '@/utils/util'
import {getTerminationPaymentDate, getOffset, calcContractParamProxy} from '@/api/calc/otcOption/calc'
import {debounce} from 'lodash'
import {setCommonSpotPrice} from '@/views/calc/otcOtion/instrument/common/common'
import {linkPercentage,linkPrice}  from '@/views/calc/otcOtion/instrument/common/formula'
const getColor = () => {
  return form.data.payDirection == '1' ? '#E53C3C' : '#22BB98'
}
 const {proxy} = getCurrentInstance()
const productType = 'MultiDigital'
const cpoyWait = ref(false)
const labelWidth = ref('111px')
const form = reactive({
  data: {
    productType: 'MultiDigital',
    payDirection: '1',
    instrumentId: null,
    optionType: 'Call',
    effectiveDate: proxy.$moment(new Date()).format('YYYY-MM-DD'),
    terminationDate: '',
    terminationPaymentDate: '',
    term: '30',
    paymentDaysOffset: '0',
    priceOnWait: '0',
    spotPrice: '',
    strikePercentage: '100',
    strike: 0,
    lowStrikePercentage: '100',
    lowStrike: 0,
    notional: '',
    optionEntitlement: '',
    participationRate: 100,
    calendar: 'CHINA_EX',
    daycounterTerm: '365',
    exerciseType: '',
    isIncludeEndDate: '',
    endDate: '',
    isIncludeStartDate: '',
    startDate: '',
    moneynessOption: '1',
    daycounter: 'Actual/365',
    paymentAmountPecentage: 100,
    paymentAmount: '',
    lowMoneynessOption: '1',
    lowPaymentAmountPecentage: 100,
    lowPaymentAmount: '',
    midPaymentAmount: '',
    midMoneynessOption: '1',
    midPaymentAmountPecentage: 100,
    fixedRate: '',
  },
  result: {},
  initFlag:''
})
const calcInvertVisible = ref(false)
const Dialog = reactive({
  showMoneynessOption: true,
  type: '',
  data: {
    valueAmount: '',
    labelAmount: '',
    valuePercentage: '',
    labelPercentage: ''
  }
})
import {computeType} from "./common/data";
import {validateDates} from "./common/validate";
const rules = {
  payDirection: [{required: true, message: '请选择交易方向', trigger: 'change'}],
  instrumentId: [{required: true, message: ' ', trigger: 'change'}],
  optionType: [{required: true, message: '请选择涨跌类型', trigger: 'change'}],
  effectiveDate: [{required: true, message: ' ', trigger: 'change'}],
  terminationDate: [{required: true, message: ' ', trigger: 'change'}],
  term: [{required: true, message: ' ', trigger: 'change'}],
  paymentDaysOffset: [{required: true, message: ' ', trigger: 'change'}],
  notional: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  daycounter: [{required: true, message: ' ', trigger: 'change'}],
  daycounterTerm: [{required: true, message: ' ', trigger: 'change'}],
  spotPrice: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      if (form.data.priceOnWait == '1') {
        return true
      }
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  optionEntitlement: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      if (form.data.priceOnWait == '1') {
        return true
      }
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  strike: [{required: true, message: ' ', trigger: 'blur'}, {
    validator: (rule, value, callback) => {
      if (form.data.priceOnWait == '1'&&Number(form.data.strikePercentage)>0) {
        return true
      }
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  strikePercentage: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      if (form.data.priceOnWait == '1'&&Number(form.data.strike)>0) {
        return true
      }
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  lowStrike: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      if (form.data.priceOnWait == '1'&&Number(form.data.lowStrikePercentage)>0) {
        return true
      }
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  lowStrikePercentage: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      if (form.data.priceOnWait == '1'&&Number(form.data.lowStrike)>0) {
        return true
      }
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  participationRate: [{required: true, message: ' ', trigger: 'change'}],
  paymentAmountPecentage: [{required: true, message: ' ', trigger: 'change'}],
  paymentAmount: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) >= 0
    }, message: ' ', trigger: 'change'
  }],
  midPaymentAmount: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) >= 0
    }, message: ' ', trigger: 'change'
  }],
  lowPaymentAmount: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) >= 0
    }, message: ' ', trigger: 'change'
  }]
}

watch(() => props.result, (nv, ov) => {
  let other = {
    'type': '三层阶梯',
    '标的': computed(() => {
      if (proxy.$refs.underlyerIdRef&&proxy.$refs.underlyerIdRef.underlyerIdsList[0]) {
        return (proxy.$refs.underlyerIdRef.underlyerIdsList[0].productName+'').trim().split(' ').join(',')
      }
    }),
    '交易日期': props.valueDate,
    '合约类型': form.data.optionType == 'Call' ? 'C' : 'P',
    '合约交易方向': form.data.payDirection == '1' ? '客户卖价' :  '客户买价',
    '到期支付日': form.data.terminationDate,
    '高行权价': formartFormP2(form.data.strike),
    '低行权价': formartFormP2(form.data.lowStrike),
    '高档收益金额': formartFormP2(form.data.paymentAmount),
    '中档收益金额': formartFormP2(form.data.midPaymentAmount),
    '低档收益金额': formartFormP2(form.data.lowPaymentAmount),
    '数量': formartFormP2(form.data.optionEntitlement),
    '名义金额': formartFormP2(form.data.notional),
    '开仓标的价': formartFormP2(form.data.spotPrice),
    '期权单价': computed(() => {
      if (nv) {
        return formartFormP2(nv.npv / form.data.optionEntitlement)
      }
    }),
    '百分比估值': computed(() => {
      if (nv) {
        return formartFormP2(100 * nv.npv / form.data.notional)
      }
    }),
  }
  form.result = Object.assign(other, nv)
}, {immediate: true})


watch(() => form.data.priceOnWait, (nv, ov) => {
  if (nv == '1') {
    form.data.spotPrice = 0
  } else {
    setSpotPrice()
  }
})
/*
* 修改状态  false:当前没有操作  true:当前在被其他方法操作
* */
const lock = reactive({
  paymentDaysOffset: false,
  terminationPaymentDate: false,
})
watch(() => form.data.paymentDaysOffset, (nv, ov) => {
  if (nv === '') return
  if (lock.terminationPaymentDate) return
  lock.terminationPaymentDate = true
  onpaymentDaysOffsetChange()
})
const onpaymentDaysOffsetChange = debounce(function () {
  getTerminationPaymentDate({
    'calDay': form.data.terminationDate,
    'offset': form.data.paymentDaysOffset
  }).then(response => {
    lock.terminationPaymentDate = false
    if (response.errorCode != 0) {

    } else {
      if (response.result) {
        form.data.terminationPaymentDate = response.result.CAL_DAY
      }
    }
  })
}, 1000)
watch(() => form.data.terminationPaymentDate, (nv, ov) => {
  if (nv == ov || nv === '') return
  if (lock.paymentDaysOffset) return
  lock.paymentDaysOffset = true
  onterminationPaymentDate()
})
const onterminationPaymentDate = debounce(function (nv, ov) {
  getOffset({
    'day': form.data.terminationPaymentDate,
    'calDay': form.data.terminationDate
  }).then(response => {
    if (response.errorCode != 0) {

    } else {
      if (response.result == null) {
        onpaymentDaysOffsetChange()
        return
      }
      form.data.paymentDaysOffset = response.result.i
      lock.paymentDaysOffset = false
    }
  })
}, 1000)
onUnmounted(() => {
  // 移除组件时，取消定时器
  onpaymentDaysOffsetChange.cancel()
  onterminationPaymentDate.cancel()
})
const onBlurTerminationDate= (nv, ov) => {
  let a = proxy.$moment(form.data.terminationDate)
  let b = proxy.$moment(form.data.terminationPaymentDate)
  form.data.paymentDaysOffset = b.diff(a, 'days')
}
watch(() => [form.data.effectiveDate, form.data.terminationDate], ([nv, n2v], [ov, o2v]) => {
  if (!proxy.lsh.isEmpty(nv) && !proxy.lsh.isEmpty(n2v)) {
    let a = proxy.$moment(n2v)
    let b = proxy.$moment(nv)
    form.data.term = Number(a.diff(b, 'days'))
   proxy.$refs.PricingParamRef?proxy.$refs.PricingParamRef.calcRateByCode():''
  }
})
watch(() => [form.data.term], ([newValue], [oldValue]) => {
  if (isNotVoid(String(form.data.effectiveDate)) && isNotVoid(String(newValue))) {
    let a = proxy.$moment(form.data.effectiveDate)
    form.data.terminationDate = a.add(newValue, 'days').format('YYYY-MM-DD')
    onpaymentDaysOffsetChange()
  }
})
/**
 * 1.1
 * 名义本金  联动 行权价 交易数量
 * */
watch(() => [form.data.notional], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (optionEntitlementLinkValid(newValue, form.data.spotPrice, form.data.optionEntitlement)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (isNotVoid(String(form.data.spotPrice))) {
      if (isEqualNum(form.data.spotPrice, 0)) {
        return
      }
      form.data.optionEntitlement = newValue / form.data.spotPrice
      return
    }
    if (isNotVoid(String(form.data.optionEntitlement))) {
      if (isEqualNum(form.data.optionEntitlement, 0)) {
        return
      }
      form.data.spotPrice = newValue / form.data.optionEntitlement
      return
    }
  }
})
/**
 * 1.2
 * 期初价格  联动 名义本金 交易数量
 * */
watch(() => [form.data.spotPrice], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (optionEntitlementLinkValid(form.data.notional, newValue, form.data.optionEntitlement)) {
    return
  }

  if (isNotVoid(String(newValue))) {
    if (isNotVoid(String(form.data.notional))) {
      if (isEqualNum(form.data.spotPrice, 0)) {
        form.data.optionEntitlement = 0
        return
      }
      form.data.optionEntitlement = form.data.notional / newValue
      return
    }
    if (isNotVoid(String(form.data.optionEntitlement))) {
      form.data.notional = form.data.optionEntitlement * newValue
      return
    }
  }
})
/**
 * 1.3
 * 交易数量 联动 期初价格 名义本金
 * */
const optionEntitlementChange = (newValue) => {
  if (form.data.priceOnWait == '1') return
  if (isVoid(newValue)) {
    return
  }
  if (optionEntitlementLinkValid(form.data.notional, form.data.spotPrice, newValue)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (isNotVoid(String(form.data.spotPrice))) {
      form.data.notional = newValue * form.data.spotPrice
      return
    }
    if (isNotVoid(String(form.data.notional))) {
      form.data.spotPrice = form.data.notional / newValue
      return
    }
  }
}

/**
 *
 * 交易数量  期初价格 名义本金 联动 等式
 * */
function optionEntitlementLinkValid(notional, spotPrice, optionEntitlement) {
  if (isNotVoid(notional) && isNotVoid((spotPrice)) && isNotVoid(optionEntitlement)) {
    return isEquativesABC(optionEntitlement, spotPrice, notional)
  }
  return false
}

/**
 * 2.1
 * 期初价格  联动 行权比例  行权价
 * */
 function spotPriceBlur() {
  linkPercentage(form,form.data.strikePercentage,form.data.spotPrice,'data.strike')
  linkPercentage(form,form.data.lowStrikePercentage,form.data.spotPrice,'data.lowStrike')
}

/**
 * 3.1  高
 *  期限 监听 高档收益率 收益金额  是否年化  名义本金  计息基准
 * */
watch(() => [form.data.term], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }

  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, form.data.paymentAmountPecentage, form.data.paymentAmount, form.data.daycounter, newValue)) {
    return
  }
  if (form.data.moneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.notional))) {

        if (isNotVoid(String(form.data.paymentAmountPecentage))) {
          if (isEqualNum(0, form.data.paymentAmount)) {
            form.data.paymentAmount = 0
            return
          } else {
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            form.data.paymentAmount = form.data.paymentAmountPecentage / 100 * annualized * form.data.notional
            return
          }
        }
        if (isNotVoid(String(form.data.paymentAmount))) {
          if (isEqualNum(0, form.data.paymentAmount)) {
            form.data.paymentAmountPecentage = 0
            return
          } else {
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            form.data.paymentAmountPecentage = form.data.paymentAmount / (annualized * form.data.notional) * 100
            return
          }
        }
      }
    }
  }

})
/**
 * 3.2  高
 *  计息基准 监听 高档收益率 收益金额  是否年化  名义本金  期限
 * */
watch(() => [form.data.daycounter], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, form.data.paymentAmountPecentage, form.data.paymentAmount, newValue, form.data.term)) {
    return
  }
  if (form.data.moneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.term))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        if (isNotVoid(String(form.data.paymentAmountPecentage))) {
          if (isEqualNum(form.data.paymentAmountPecentage, 0)) {
            form.data.paymentAmount = 0
            return
          } else {
            form.data.paymentAmount = form.data.paymentAmountPecentage / 100 * annualized * form.data.notional
            return
          }
        }
        if (isVoid(String(form.data.paymentAmountPecentage)) && isNotVoid(String(form.data.paymentAmount))) {
          if (isEqualNum(form.data.paymentAmount, 0)) {
            form.data.paymentAmountPecentage = 0
            return
          } else {
            form.data.paymentAmountPecentage = form.data.paymentAmount / (annualized * form.data.notional) * 100
            return
          }
        }
      }
    } else {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.paymentAmountPecentage))) {

        if (isEqualNum(form.data.paymentAmountPecentage, 0)) {
          form.data.paymentAmount = 0
          return
        } else {
          form.data.paymentAmount = form.data.paymentAmountPecentage / 100 * form.data.notional
          return
        }
      }
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.paymentAmount))) {
        if (isEqualNum(form.data.paymentAmount, 0)) {
          form.data.paymentAmountPecentage = 0
          return
        } else {
          form.data.paymentAmountPecentage = form.data.paymentAmount / form.data.notional * 100
          return
        }
      }
    }
  }

})
/**
 * 3.3  高
 *  收益金额 监听 高档收益率 计息基准  是否年化  名义本金  期限
 * */
watch(() => [form.data.paymentAmount], ([newValue], [oldValue]) => {
  return
  if (isVoid(newValue)) {
    return
  }

  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, form.data.paymentAmountPecentage, newValue, form.data.daycounter, form.data.term)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (form.data.moneynessOption == '1') {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (isEqualNum(0, newValue)) {
          form.data.paymentAmountPecentage = 0.00
          return
        } else {
          if (isEqualNum(0, form.data.notional)) {
            form.data.paymentAmountPecentage = 0
          } else {
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            let percentages = newValue / (annualized * form.data.notional) * 100
            form.data.paymentAmountPecentage = percentages * 100
            return
          }
        }
      }
      if (isNotVoid(String(form.data.paymentAmountPecentage)) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (isEqualNum(0, newValue)) {
          form.data.notional = 0.00
          return
        } else {
          if (isEqualNum(0, form.data.paymentAmountPecentage)) {
            form.data.notional = 0
          } else {
            let percentages = form.data.paymentAmountPecentage / 100
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            form.data.notional = (newValue / percentages) / annualized
            return
          }
        }

      }

    } else {
      if (isNotVoid(String(form.data.notional))) {
        form.data.paymentAmountPecentage = newValue * 100 / form.data.notional
        return
      }
      if (isNotVoid(String(form.data.paymentAmountPecentage))) {
        if (isEqualNum(0, form.data.paymentAmountPecentage)) {
          form.data.notional = 0
        } else {
          let percentages = form.data.paymentAmountPecentage / 100
          form.data.notional = newValue / percentages
          return
        }
      }

    }
  }
})
/**
 * 3.4  高
 *  收益金额 监听 高档收益率 计息基准  是否年化  名义本金  期限
 * */
const onPaymentAmountChange = () => {
  let newValue = form.data.paymentAmount
  if (isVoid(newValue)) {
    form.data.paymentAmountPecentage = 0.00
    return
  }

  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, form.data.paymentAmountPecentage, newValue, form.data.daycounter, form.data.term)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (form.data.moneynessOption == '1') {
      if (isNotVoid(String(form.data.notional)) && !isEqualNum(form.data.notional, 0) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (isEqualNum(0, newValue)) {
          form.data.paymentAmountPecentage = 0.00
          return
        } else {
          if (isEqualNum(0, form.data.notional)) {
            form.data.paymentAmountPecentage = 0
          } else {
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            let percentages = 100 * newValue / (annualized * form.data.notional)
            form.data.paymentAmountPecentage = percentages
            return
          }
        }
      }
      if (isNotVoid(String(form.data.paymentAmountPecentage)) && !isEqualNum(form.data.paymentAmountPecentage, 0) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (isEqualNum(0, newValue)) {
          form.data.notional = 0.00
          return
        } else {
          if (isEqualNum(0, form.data.paymentAmountPecentage)) {
            form.data.notional = 0
          } else {
            let percentages = form.data.paymentAmountPecentage / 100
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            form.data.notional = (newValue / percentages) / annualized
            return
          }
        }

      }
    } else {
      if (isNotVoid(String(form.data.notional)) && !isEqualNum(form.data.notional, 0)) {
        form.data.paymentAmountPecentage = newValue * 100 / form.data.notional
        return
      }
      if (isNotVoid(String(form.data.paymentAmountPecentage)) && !isEqualNum(form.data.paymentAmountPecentage, 0)) {
        if (isEqualNum(0, form.data.paymentAmountPecentage)) {
          form.data.notional = 0
        } else {
          let percentages = form.data.paymentAmountPecentage / 100
          form.data.notional = newValue / percentages
          return
        }
      }

    }
  }
}
/**
 * 3.5  高
 *  高档收益率  监听 收益金额 计息基准  是否年化  名义本金  期限
 * */
const onPaymentAmountPecentageChange = () => {
  let newValue = form.data.paymentAmountPecentage
  if (isVoid(newValue)) {
    return
  }

  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, newValue, form.data.paymentAmount, form.data.daycounter, form.data.term)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (form.data.moneynessOption == '1') {
      if (isNotVoid(String(form.data.notional)) && !isEqualNum(form.data.notional, 0) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term)) && isNotVoid(String(newValue))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        form.data.paymentAmount = newValue / 100 * annualized * form.data.notional
        return
      }
      if (isNotVoid(String(form.data.paymentAmount)) && !isEqualNum(form.data.paymentAmount, 0) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (isEqualNum(0, newValue)) {
          form.data.notional = 0.00
          return
        } else {
          let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
          form.data.notional = form.data.paymentAmount / newValue * 100 / annualized
          return
        }
      }

    } else {
      if (isNotVoid(String(form.data.notional)) && !isEqualNum(form.data.notional, 0)) {
        if (isEqualNum(0, newValue)) {
          form.data.paymentAmount = 0.00
          return
        } else {
          form.data.paymentAmount = form.data.notional * newValue / 100
          return
        }
      }
      if (isNotVoid(String(form.data.paymentAmount)) && !isEqualNum(form.data.paymentAmount, 0)) {
        if (isEqualNum(0, newValue)) {
          form.data.notional = 0.00
          return
        } else {
          form.data.notional = form.data.paymentAmount / newValue * 100
          return
        }
      }

    }
  }
}
/**
 * 3.6 高
 * 名义本金 监听 高档收益率 收益金额  是否年化 计息基准  期限
 * */
watch(() => [form.data.notional], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }

  if (paymentAmountLinkValid(form.data.moneynessOption, newValue, form.data.paymentAmountPecentage, form.data.paymentAmount, form.data.daycounter, form.data.term)) {
    return
  }
  if (form.data.moneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        if (isNotVoid(form.data.paymentAmountPecentage) && isVoid(String(form.data.paymentAmount))) {
          if (isEqualNum(0, form.data.paymentAmountPecentage)) {
            form.data.paymentAmount = 0
            return
          } else {
            let percentages = form.data.paymentAmountPecentage / 100
            form.data.paymentAmount = percentages * annualized * newValue
            return
          }
        }
        if (isNotVoid(String(form.data.paymentAmountPecentage)) && isNotVoid(String(form.data.paymentAmount))) {
          if (isEqualNum(0, form.data.paymentAmountPecentage)) {
            form.data.paymentAmount = 0
            return
          } else {
            let percentages = form.data.paymentAmountPecentage / 100
            form.data.paymentAmount = percentages * annualized * newValue
            return
          }
        }
        if (isVoid(String(form.data.paymentAmountPecentage)) && isNotVoid(String(form.data.paymentAmount))) {
          if (isEqualNum(0, form.data.paymentAmount)) {
            form.data.paymentAmountPecentage = 0
            return
          } else {
            form.data.paymentAmountPecentage = 100 * form.data.paymentAmount / (annualized * newValue)
            return
          }
        }
      }
    }
  } else {
    if (isNotVoid(String(form.data.paymentAmountPecentage))) {
      if (isEqualNum(0, form.data.paymentAmountPecentage)) {
        form.data.paymentAmount = 0
        return
      } else {
        form.data.paymentAmount = form.data.paymentAmountPecentage / 100 * newValue
        return
      }
    }
  }
})

/**
 * 3.7 高
 *   是否年化 监听  行权收益率 收益金额   名义本金  计息基准
 * */
watch(() => [form.data.moneynessOption], ([newValue], [oldValue]) => {
  if (paymentAmountLinkValid(newValue, form.data.notional, form.data.paymentAmountPecentage, form.data.paymentAmount, form.data.daycounter, form.data.term)) {
    return
  }
  if (newValue == '1') {
    if (isNotVoidArg(String(form.data.notional), String(form.data.daycounter), String(form.data.term))) {
      let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
      if (isNotVoid(String(form.data.paymentAmountPecentage))) {
        if (!isEqualNum(0, form.data.paymentAmountPecentage) && !isEqualNum(0, form.data.paymentAmount)) {
          form.data.paymentAmount = form.data.paymentAmountPecentage / 100 * annualized * form.data.notional
          return
        } else {
          form.data.paymentAmount = 0
          return
        }
      }
      if (isNotVoid(String(form.data.paymentAmount))) {
        if (!isEqualNum(0, form.data.paymentAmount) && !isEqualNum(0, form.data.paymentAmount)) {
          form.data.paymentAmountPecentage = 100 * form.data.paymentAmount / (annualized * form.data.notional)
          return
        } else {
          form.data.paymentAmountPecentage = 0
          return
        }
      }

    }
  } else {
    if (isNotVoidArg(String(form.data.notional), String(form.data.paymentAmountPecentage))) {
      if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, form.data.notional)) {
        form.data.paymentAmount = form.data.paymentAmountPecentage / 100 * form.data.notional
        return
      } else {
        form.data.paymentAmount = 0
        return
      }
    }
    if (isNotVoid(String(form.data.notional), String(form.data.paymentAmount))) {
      if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, form.data.paymentAmount)) {
        form.data.paymentAmountPecentage = 100 * form.data.paymentAmount / form.data.notional
        return
      } else {
        form.data.paymentAmountPecentage = 0
        return
      }
    }
  }

})

/**
 *
 *  高档收益率 收益金额 计息基准  是否年化  名义本金  期限  等式
 * */
function paymentAmountLinkValid(moneynessOption, notional, paymentAmountPecentage, paymentAmount, daycounter, term) {
  if (moneynessOption == '1') {
    if (isNotVoid(String(notional)) && isNotVoid(String(paymentAmountPecentage)) && isNotVoid(String(paymentAmount)) && isNotVoid(String(term))) {
      let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
      let actualPercentages = paymentAmountPecentage / 100
      var isTrue = isEquativesAnnualizedABC(notional, actualPercentages, annualized, paymentAmount)
      return isTrue
    }
  } else {
    if (isNotVoid(String(notional)) && isNotVoid(String(paymentAmountPecentage)) && isNotVoid(String(paymentAmount))) {
      let actualPercentages = paymentAmountPecentage / 100
      var isTrue = isEquativesABC(actualPercentages, notional, paymentAmount)
      return isTrue
    }
  }
}

/**
 * 4.1  中
 *  期限 监听 高档收益率 收益金额  是否年化  名义本金  计息基准
 * */
watch(() => [form.data.term], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }

  if (paymentAmountLinkValid(form.data.midMoneynessOption, form.data.notional, form.data.midPaymentAmountPecentage, form.data.midPaymentAmount, form.data.daycounter, newValue)) {
    return
  }
  if (form.data.midMoneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.notional))) {

        if (isNotVoid(String(form.data.midPaymentAmountPecentage))) {
          if (isEqualNum(0, form.data.midPaymentAmount)) {
            form.data.midPaymentAmount = 0
            return
          } else {
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            form.data.midPaymentAmount = form.data.midPaymentAmountPecentage / 100 * annualized * form.data.notional
            return
          }
        }
        if (isNotVoid(String(form.data.midPaymentAmount))) {
          if (isEqualNum(0, form.data.midPaymentAmount)) {
            form.data.midPaymentAmountPecentage = 0
            return
          } else {
            form.data.midPaymentAmountPecentage = 100 * form.data.midPaymentAmount / (annualized * form.data.notional)
            return
          }
        }
      }
    }
  }

})
/**
 * 4.2  中
 *  计息基准 监听 高档收益率 收益金额  是否年化  名义本金  期限
 * */
watch(() => [form.data.daycounter], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.midMoneynessOption, form.data.notional, form.data.midPaymentAmountPecentage, form.data.midPaymentAmount, newValue, form.data.term)) {
    return
  }
  if (form.data.midMoneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.term))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        if (isNotVoid(String(form.data.midPaymentAmountPecentage))) {
          if (isEqualNum(form.data.midPaymentAmountPecentage, 0)) {
            form.data.midPaymentAmount = 0
            return
          } else {
            form.data.midPaymentAmount = form.data.midPaymentAmountPecentage / 100 * (annualized * form.data.notional)
            return
          }
        }
        if (isNotVoid(String(form.data.midPaymentAmount))) {
          if (isEqualNum(form.data.midPaymentAmount, 0)) {
            form.data.midPaymentAmountPecentage = 0
            return
          } else {
            form.data.midPaymentAmountPecentage = form.data.midPaymentAmount * 100 / (annualized * form.data.notional)
            return
          }
        }
      }
    } else {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.midPaymentAmountPecentage))) {
        if (isEqualNum(form.data.midPaymentAmountPecentage, 0)) {
          form.data.midPaymentAmount = 0
          return
        } else {
          form.data.midPaymentAmount = form.data.midPaymentAmountPecentage / 100 * form.data.notional
          return
        }
      }
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.midPaymentAmount))) {
        if (isEqualNum(form.data.midPaymentAmount, 0)) {
          form.data.midPaymentAmountPecentage = 0
          return
        } else {
          form.data.midPaymentAmountPecentage = 100 * form.data.midPaymentAmount / form.data.notional
          return
        }
      }
    }
  }

})
/**
 * 4.3  中
 *  低档收益金额 监听 高档收益率 计息基准  是否年化  名义本金  期限
 * */
const onMidPaymentAmountChange = () => {
  let newValue = form.data.midPaymentAmount
  if (isVoid(newValue)) {
    form.data.midPaymentAmountPecentage = 0
    return
  }

  if (paymentAmountLinkValid(form.data.midMoneynessOption, form.data.notional, form.data.midPaymentAmountPecentage, newValue, form.data.daycounter, form.data.term)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (form.data.midMoneynessOption == '1') {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, newValue)) {
          let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
          let percentages = 100 * newValue / (annualized * form.data.notional)
          form.data.midPaymentAmountPecentage = percentages
          return
        } else {
          form.data.midPaymentAmountPecentage = 0
          return
        }
      }
    } else {
      if (isNotVoid(String(form.data.notional))) {
        if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, newValue)) {
          form.data.midPaymentAmountPecentage = 100 * newValue / form.data.notional
          return
        } else {
          form.data.midPaymentAmountPecentage = 0
          return
        }
      }
    }
  }
}
/**
 * 4.4  中
 *  低档收益率  监听 收益金额 计息基准  是否年化  名义本金  期限
 * */

const onMidPaymentAmountPecentageChange = () => {
  let newValue = form.data.midPaymentAmountPecentage
  if (isVoid(newValue)) {
    return
  }

  if (paymentAmountLinkValid(form.data.midMoneynessOption, form.data.notional, newValue, form.data.midPaymentAmount, form.data.daycounter, form.data.term)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (form.data.midMoneynessOption == '1') {

      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term)) && isNotVoid(String(newValue))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        form.data.midPaymentAmount = newValue / 100 * annualized * form.data.notional
        return
      }
    } else {
      if (isNotVoid(String(form.data.notional))) {
        if (isEqualNum(0, newValue)) {
          form.data.midPaymentAmount = 0.00
          return
        } else {
          form.data.midPaymentAmount = form.data.notional * newValue / 100
          return
        }
      }
    }
  }
}
/**
 * 4.5 中
 * 名义本金 监听 高档收益率 收益金额  是否年化 计息基准  期限
 * */
watch(() => [form.data.notional], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.midMoneynessOption, newValue, form.data.midPaymentAmountPecentage, form.data.midPaymentAmount, form.data.daycounter, form.data.term)) {
    return
  }
  if (form.data.midMoneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        if (isNotVoid(form.data.midPaymentAmountPecentage)) {
          if (isEqualNum(0, form.data.midPaymentAmountPecentage)) {
            form.data.midPaymentAmount = 0
            return
          } else {
            let percentages = form.data.midPaymentAmountPecentage / 100
            form.data.midPaymentAmount = percentages * annualized * newValue
            return
          }
        }
        if (isNotVoid(String(form.data.midPaymentAmount))) {
          if (isEqualNum(0, form.data.midPaymentAmount)) {
            form.data.midPaymentAmountPecentage = 0
            return
          } else {
            form.data.midPaymentAmountPecentage = form.data.midPaymentAmount * 100 / (annualized * newValue)
            return
          }
        }
      }
    }
  } else {
    if (isNotVoid(String(form.data.midPaymentAmountPecentage))) {
      if (isEqualNum(0, form.data.midPaymentAmountPecentage)) {
        form.data.midPaymentAmount = 0
        return
      } else {
        form.data.midPaymentAmount = form.data.midPaymentAmountPecentage / 100 * newValue
        return
      }
    }
  }
})

/**
 * 4.6  中
 *   是否年化 监听  行权收益率 收益金额   名义本金  计息基准
 * */
watch(() => [form.data.midMoneynessOption], ([newValue], [oldValue]) => {
  if (paymentAmountLinkValid(newValue, form.data.notional, form.data.midPaymentAmountPecentage, form.data.midPaymentAmount, form.data.daycounter, form.data.term)) {
    return
  }
  if (newValue == '1') {
    if (isNotVoidArg(String(form.data.notional), String(form.data.daycounter), String(form.data.term))) {
      let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
      if (isNotVoid(String(form.data.midPaymentAmountPecentage))) {
        if (isEqualNum(0, form.data.midPaymentAmountPecentage)) {
          form.data.midPaymentAmount = 0
          return
        } else {
          form.data.midPaymentAmount = form.data.midPaymentAmountPecentage / 100 * annualized * form.data.notional
          return
        }
      }
      if (isNotVoid(String(form.data.midPaymentAmount))) {
        if (isEqualNum(0, form.data.midPaymentAmount)) {
          form.data.midPaymentAmountPecentage = 0
          return
        } else {
          form.data.midPaymentAmountPecentage = 100 * form.data.midPaymentAmount / (annualized * form.data.notional)
          return
        }
      }

    }
  } else {
    if (isNotVoidArg(String(form.data.notional), String(form.data.midPaymentAmountPecentage))) {
      if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, form.data.notional)) {
        form.data.midPaymentAmount = form.data.midPaymentAmountPecentage / 100 * form.data.notional
        return
      } else {
        form.data.midPaymentAmount = 0
        return
      }
    }
    if (isNotVoidArg(String(form.data.midPaymentAmount), String(form.data.midPaymentAmountPecentage))) {
      if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, form.data.notional)) {
        form.data.midPaymentAmountPecentage = 100 * form.data.midPaymentAmount / form.data.notional
        return
      } else {
        form.data.midPaymentAmountPecentage = 0
        return
      }
    }
  }

})


/**
 * 5.1  低
 *  期限 监听 高档收益率 收益金额  是否年化  名义本金  计息基准
 * */
watch(() => [form.data.term], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }

  if (paymentAmountLinkValid(form.data.lowMoneynessOption, form.data.notional, form.data.lowPaymentAmountPecentage, form.data.lowPaymentAmount, form.data.daycounter, newValue)) {
    return
  }
  if (form.data.lowMoneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.notional))) {

        if (isNotVoid(String(form.data.lowPaymentAmountPecentage))) {
          if (isEqualNum(0, form.data.lowPaymentAmount)) {
            form.data.lowPaymentAmount = 0
            return
          } else {
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            form.data.lowPaymentAmount = form.data.lowPaymentAmountPecentage / 100 * annualized * form.data.notional
            return
          }
        }
        if (isNotVoid(String(form.data.lowPaymentAmount))) {
          if (isEqualNum(0, form.data.lowPaymentAmount)) {
            form.data.lowPaymentAmountPecentage = 0
            return
          } else {
            form.data.lowPaymentAmountPecentage = form.data.lowPaymentAmount / (annualized * form.data.notional) * 100
            return
          }
        }
      }
    }
  }

})
/**
 * 5.2  低
 *  计息基准 监听 高档收益率 收益金额  是否年化  名义本金  期限
 * */
watch(() => [form.data.daycounter], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.lowMoneynessOption, form.data.notional, form.data.lowPaymentAmountPecentage, form.data.lowPaymentAmount, newValue, form.data.term)) {
    return
  }
  if (form.data.lowMoneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.term))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        if (isNotVoid(String(form.data.lowPaymentAmountPecentage))) {
          if (isEqualNum(form.data.lowPaymentAmountPecentage, 0)) {
            form.data.lowPaymentAmount = 0
            return
          } else {
            form.data.lowPaymentAmount = form.data.lowPaymentAmountPecentage / 100 * (annualized * form.data.notional)
            return
          }
        }
        if (isNotVoid(String(form.data.lowPaymentAmount))) {
          if (isEqualNum(form.data.lowPaymentAmount, 0)) {
            form.data.lowPaymentAmountPecentage = 0
            return
          } else {
            form.data.lowPaymentAmountPecentage = form.data.lowPaymentAmount * 100 / (annualized * form.data.notional)
            return
          }
        }
      }
    } else {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.lowPaymentAmountPecentage))) {
        if (isEqualNum(form.data.lowPaymentAmountPecentage, 0)) {
          form.data.lowPaymentAmount = 0
          return
        } else {
          form.data.lowPaymentAmount = form.data.lowPaymentAmountPecentage / 100 * form.data.notional
          return
        }
      }
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.lowPaymentAmount))) {
        if (isEqualNum(form.data.lowPaymentAmount, 0)) {
          form.data.lowPaymentAmountPecentage = 0
          return
        } else {
          form.data.lowPaymentAmountPecentage = form.data.lowPaymentAmount / form.data.notional * 100
          return
        }
      }
    }
  }

})
/**
 * 5.3  低
 *  低档收益金额 监听 高档收益率 计息基准  是否年化  名义本金  期限
 * */
const onLowPaymentAmountChange = () => {
  let newValue = form.data.lowPaymentAmount
  if (isVoid(newValue)) {
    form.data.lowPaymentAmountPecentage = 0
    return
  }

  if (paymentAmountLinkValid(form.data.lowMoneynessOption, form.data.notional, form.data.lowPaymentAmountPecentage, newValue, form.data.daycounter, form.data.term)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (form.data.lowMoneynessOption == '1') {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, newValue)) {
          let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
          let percentages = 100 * newValue / (annualized * form.data.notional)
          form.data.lowPaymentAmountPecentage = percentages
          return
        } else {
          form.data.lowPaymentAmountPecentage = 0
          return
        }
      }
    } else {
      if (isNotVoid(String(form.data.notional))) {
        if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, newValue)) {
          form.data.lowPaymentAmountPecentage = 100 * newValue / form.data.notional
          return
        } else {
          form.data.lowPaymentAmountPecentage = 0
          return
        }
      }
    }
  }
}
/**
 * 5.4  低
 *  低档收益率  监听 收益金额 计息基准  是否年化  名义本金  期限
 * */
const onLowPaymentAmountPecentageChange = () => {
  let newValue = form.data.lowPaymentAmountPecentage
  if (isVoid(newValue)) {
    return
  }

  if (paymentAmountLinkValid(form.data.lowMoneynessOption, form.data.notional, newValue, form.data.lowPaymentAmount, form.data.daycounter, form.data.term)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (form.data.lowMoneynessOption == '1') {

      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term)) && isNotVoid(String(newValue))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        form.data.lowPaymentAmount = newValue * annualized * form.data.notional / 100
        return
      }
    } else {
      if (isNotVoid(String(form.data.notional))) {
        if (isEqualNum(0, newValue)) {
          form.data.lowPaymentAmount = 0.00
          return
        } else {
          form.data.lowPaymentAmount = form.data.notional * newValue / 100
          return
        }
      }
    }
  }
}
/**
 * 5.5 低
 * 名义本金 监听 高档收益率 收益金额  是否年化 计息基准  期限
 * */
watch(() => [form.data.notional], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.lowMoneynessOption, newValue, form.data.lowPaymentAmountPecentage, form.data.lowPaymentAmount, form.data.daycounter, form.data.term)) {
    return
  }
  if (form.data.lowMoneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        if (isNotVoid(form.data.lowPaymentAmountPecentage)) {
          if (isEqualNum(0, form.data.lowPaymentAmountPecentage)) {
            form.data.lowPaymentAmount = 0
            return
          } else {
            let percentages = form.data.lowPaymentAmountPecentage / 100
            form.data.lowPaymentAmount = percentages * annualized * newValue
            return
          }
        }
        if (isNotVoid(String(form.data.lowPaymentAmount))) {
          if (isEqualNum(0, form.data.lowPaymentAmount)) {
            form.data.lowPaymentAmountPecentage = 0
            return
          } else {
            form.data.lowPaymentAmountPecentage = 100 * form.data.lowPaymentAmount / (annualized * newValue)
            return
          }
        }
      }
    }
  } else {
    if (isNotVoid(String(form.data.lowPaymentAmountPecentage))) {
      if (isEqualNum(0, form.data.lowPaymentAmountPecentage)) {
        form.data.lowPaymentAmount = 0
        return
      } else {
        form.data.lowPaymentAmount = form.data.lowPaymentAmountPecentage / 100 * newValue
        return
      }
    }
  }
})

//反算逻辑
const calcInvert = () => {
  let json = convertJson();
  let {newNpv, yearnpvPercentage, npvPercentage, showSwitch} = proxy.$refs.resultCompRef.getData();
  if (showSwitch) {
    if (isVoid(yearnpvPercentage)) {
      proxy.$modal.msgWarning("请填入npv%");
      return;
    }
  } else {
    if (isVoid(npvPercentage)) {
      proxy.$modal.msgWarning("请填入npv%");
      return;
    }
  }
  json.otcOption.calcPricingParam["marketNPV"] = form.data.notional * npvPercentage / 100;
  let eleArr = [form.data.strikePercentage, form.data.lowStrikePercentage, form.data.paymentAmountPecentage, form.data.midPaymentAmountPecentage,form.data.lowPaymentAmountPecentage];
  let zeroItems = eleArr.filter(item => item != '0.00').length
  switch (zeroItems) {
    case 5: {
      proxy.$modal.msgWarning("请空出反算要素");
      return;
    }
      ;
      case 3: {
      proxy.$modal.msgWarning("请填入适当反算要素");
      return;
    }
      ;
      case 2: {
      proxy.$modal.msgWarning("请填入适当反算要素");
      return;
    }
      ;
      break
    case 1: {
      proxy.$modal.msgWarning("请填入适当反算要素");
      return;
    }
      ;
      break
  }
  //高行权价/比例
  if (form.data.strikePercentage == '0.00') {
    json.otcOption["contractRoots"] = {
      "contractRoot": [
        {
          "computeType": computeType.CAP_PRICE,
          "index": 1
        }, {
          "computeType": computeType.FLOOR_PRICE,
          "index": 2
        }
      ]
    }
    json.otcOption["sectionSolvers"] =
        {"cap":2* form.data.spotPrice,"floor":form.data.lowStrike,"step":2}
    json.otcOption.instrument.termsheet.customLegs[0].priceRanges[1].capPrice = form.data.lowStrike
    json.otcOption.instrument.termsheet.customLegs[0].priceRanges[2].floorPrice = form.data.lowStrike
    calcContractParamProxy(json.otcOption).then((result) => {
      let amounts = result.amounts.double;
      let ratios = result.ratios.double;
      if (amounts.length > 1) {
        //todo 多个返回值时舍去值的逻辑

      }
      let amountsTarget = amounts[0];
      let ratiosTarget = ratios[0];

      calcInvertVisible.value = true
      Dialog.type = "strikePercentage"
      Dialog.showMoneynessOption = false
      form.data.strike = amountsTarget
      form.data.strikePercentage = ratiosTarget * 100
      Dialog.data.valueAmount = amountsTarget;
      Dialog.data.valuePercentage = form.data.strikePercentage;
      Dialog.data.labelAmount = "高行权价"
      Dialog.data.labelPercentage = "高行权价比例"

      proxy.$modal.msgSuccess("计算成功！");
    })
  }

  //低行权价/比例
  if (form.data.lowStrikePercentage == '0.00') {
    json.otcOption["contractRoots"] = {
      "contractRoot": [
        {
          "computeType": computeType.CAP_PRICE,
          "index": 0
        }, {
          "computeType": computeType.FLOOR_PRICE,
          "index": 1
        }
      ]
    }
    json.otcOption["sectionSolvers"] =
        {"cap": form.data.strike,"floor":0.01 * form.data.spotPrice,"step":2}
    calcContractParamProxy(json.otcOption).then((result) => {
      let amounts = result.amounts.double;
      let ratios = result.ratios.double;
      if (amounts.length > 1) {
        //todo 多个返回值时舍去值的逻辑

      }
      let amountsTarget = amounts[0];
      let ratiosTarget = ratios[0];

      calcInvertVisible.value = true
      Dialog.type = "lowStrikePercentage"
      Dialog.showMoneynessOption = false
      form.data.lowStrike = amountsTarget
      form.data.lowStrikePercentage = ratiosTarget * 100
      Dialog.data.valueAmount = amountsTarget;
      Dialog.data.valuePercentage = form.data.lowStrikePercentage;
      Dialog.data.labelAmount = "低行权价"
      Dialog.data.labelPercentage = "低行权价比例"

      proxy.$modal.msgSuccess("计算成功！");
    })
  }
  // 高档收益率/金额
  if (form.data.paymentAmountPecentage == '0.00') {
    json.otcOption["contractRoots"] = {
      "contractRoot": [
        {
          "computeType": computeType.RANGE_PAYOFF,
          "index": form.data.optionType == 'Call'? 2 : 0
        }
      ]
    }
    calcContractParamProxy(json.otcOption).then((result) => {
      let amounts = result.amounts.double;
      let ratios = result.ratios.double;
      if (amounts.length > 1) {
        //todo 多个返回值时舍去值的逻辑

      }
      let amountsTarget = amounts[0];
      let ratiosTarget = ratios[0];
      Dialog.showMoneynessOption = true
      calcInvertVisible.value = true
      Dialog.type = "paymentAmountPecentage"

      form.data.paymentAmount = amountsTarget
      form.data.paymentAmountPecentage = ratiosTarget * 100
      Dialog.data.valueAmount = form.data.paymentAmount
      Dialog.data.valuePercentage = form.data.paymentAmountPecentage
      Dialog.data.moneynessOption = form.data.moneynessOption
      Dialog.data.labelAmount = "高档收益金额"
      Dialog.data.labelPercentage = "高档收益率"
    })
  }

  // 中档收益率/金额
  if (form.data.midPaymentAmountPecentage == '0.00') {
    json.otcOption["contractRoots"] = {
      "contractRoot": [
        {
          "computeType": computeType.RANGE_PAYOFF,
          "index": 1
        }
      ]
    }
    calcContractParamProxy(json.otcOption).then((result) => {
      let amounts = result.amounts.double;
      let ratios = result.ratios.double;
      if (amounts.length > 1) {
        //todo 多个返回值时舍去值的逻辑

      }
      let amountsTarget = amounts[0];
      let ratiosTarget = ratios[0];
      Dialog.showMoneynessOption = true
      calcInvertVisible.value = true
      Dialog.type = "midPaymentAmountPecentage"

      form.data.midPaymentAmount = amountsTarget
      form.data.midPaymentAmountPecentage = ratiosTarget * 100
      Dialog.data.valueAmount = form.data.midPaymentAmount
      Dialog.data.valuePercentage = form.data.midPaymentAmountPecentage
      Dialog.data.moneynessOption = form.data.moneynessOption
      Dialog.data.labelAmount = "中档收益金额"
      Dialog.data.labelPercentage = "中档收益率"
    })
  }

  // 低档收益率/金额
  if (form.data.lowPaymentAmountPecentage == '0.00') {

    json.otcOption["contractRoots"] = {
      "contractRoot": [
        {
          "computeType": computeType.RANGE_PAYOFF,
          "index": form.data.optionType == 'Call'? 0 : 2
        }
      ]
    }
    calcContractParamProxy(json.otcOption).then((result) => {
      let amounts = result.amounts.double;
      let ratios = result.ratios.double;
      if (amounts.length > 1) {
        //todo 多个返回值时舍去值的逻辑

      }
      let amountsTarget = amounts[0];
      let ratiosTarget = ratios[0];
      Dialog.showMoneynessOption = true
      calcInvertVisible.value = true
      Dialog.type = "lowPaymentAmountPecentage"
      form.data.lowPaymentAmount = amountsTarget
      form.data.lowPaymentAmountPecentage = ratiosTarget * 100
      Dialog.data.valueAmount = form.data.lowPaymentAmount
      Dialog.data.valuePercentage = form.data.lowPaymentAmountPecentage
      Dialog.data.moneynessOption = form.data.lowMoneynessOption
      Dialog.data.labelAmount = "低档收益金额"
      Dialog.data.labelPercentage = "低档收益率"
    })
  }
}

function convertJson() {  
  let calcPriceParam = getCalcPriceParam()
  let otcOption = {
    instrument: {
      'productType': 'MultilayerDigital',
      'initProductType': 'MultiDigital',
      'termsheet': {
        'notional': form.data.notional,
        'notionalCurrency': 'CNY',
        'settlementType': 'Cash',
        'settlementCurrency': 'CNY',
        'effectiveDate': form.data.effectiveDate,
        'effectiveDateConv': 'Following',
        'terminationDate': form.data.terminationDate,
        'terminationDateConv': 'Following',
        'terminationDateCalendar': form.data.calendar,
        'term': form.data.term,
        'underlyerIds': [
          form.data.instrumentId
        ],
        'calendars': [
          form.data.calendar
        ],
        'legs': [
          {
            'legId': 'leg01',
            'payDirection': form.data.payDirection
          }
        ],
        'customLegs': [
          {
            'legId': 'leg01',
            periodOfAccrualInterestAdjust: '0',
            'payDirection': form.data.payDirection,
            'participationRate': String(form.data.participationRate / 100),
            'productType': 'MultilayerDigital',
            'optionType': form.data.optionType,
            'exercise': {
              'type': 'European',
              'dates': [
                form.data.terminationDate
              ],
              'conv': 'NONE',
              'calendar': form.data.calendar
            },
            'paymentDateCalendar': form.data.calendar,
            'underlyers': [
              {
                'instrumentId': form.data.instrumentId,
                'quoteType': '',
                'spotPrice': form.data.spotPrice,
                'basketWeight': '1',
                'fixingDate': '',
                'fixingDateConv': 'FOLLOWING',
                'fixingDateCalendar': form.data.calendar
              }
            ],
            "priceRanges": [
            {
              "capPrice": form.data.lowStrike,
              "capPricePecentage": proxy.lsh.round(form.data.lowStrikePercentage / 100, 4),
              "isIncludeCap": "",
              "floorPrice":"0",
              "floorPricePecentage": "",
              "isIncludeFloor": "",
              "daycounter":  (form.data.optionType == 'Call'?form.data.lowMoneynessOption :form.data.moneynessOption ) == '1'? form.data.daycounter : '',
              "payoff": form.data.optionType == 'Call' ? form.data.lowPaymentAmount : form.data.paymentAmount,
              "payoffRate": "",
              "participationRate": String(form.data.participationRate / 100)
            },
            {
              "capPrice": form.data.strike,
              "capPricePecentage": proxy.lsh.round(form.data.strikePercentage / 100, 4),
              "isIncludeCap": "",
              "floorPrice": form.data.lowStrike,
              "floorPricePecentage": proxy.lsh.round(form.data.lowStrikePercentage / 100, 4),
              "isIncludeFloor": "",
              "daycounter": form.data.midMoneynessOption=='1'?form.data.daycounter:'',
              "payoff": form.data.midPaymentAmount,
              "payoffRate": "",
              "participationRate": String(form.data.participationRate / 100)
            },
            {
              "capPrice": "",
              "capPricePecentage": "",
              "isIncludeCap": "",
              "floorPrice": form.data.strike,
              "floorPricePecentage": proxy.lsh.round(form.data.strikePercentage / 100, 4),
              "isIncludeFloor": "",
              "daycounter": (form.data.optionType == 'Call'?form.data.moneynessOption :form.data.lowMoneynessOption )== '1'? form.data.daycounter:'',
              "payoff": form.data.optionType == 'Call' ? form.data.paymentAmount : form.data.lowPaymentAmount,
              "payoffRate": "",
              "participationRate": String(form.data.participationRate / 100)
            }
            ],
            "calclationPeriod": {
                "startDate":form.data.effectiveDate,
                "startDateConv": "FOLLOWING",
                "startDateOffset": "",
                "isIncludeStartDate": "0",
                "endDate": form.data.terminationDate,
                "endDateConv": "FOLLOWING",
                "endDateOffset": "",
                "calendar": form.data.calendar,
                "isIncludeEndDate": "1"
              },
            'numberOfOptions': '1',
            // 'optionEntitlement': form.data.optionEntitlement,
            'moneynessOption': form.data.moneynessOption,
            'daycounter': form.data.moneynessOption == '1' ? form.data.daycounter : '',
            'fixingRounding': '-1',
            "settlementPrecision": {
              "type": "",
              "OriginReturnPrecision": "",
              "AnnualAdjustIncludedPrecision": "",
              "PartiRateIncludedPrecision": "",
              "AmountPrecision": ""
            },
            "settlementDate": "",
            'settlementDateConv': 'Following',
            'settlementDateCalendar': form.data.calendar,
            'settlementDateOffset': form.data.paymentDaysOffset + 'D',
          },
        ],
      },
    }, calcPricingParam: {
          valueDate: props.valueDate,
          cfgId: props.cfgId,
          calcPPCode: calcPriceParam.calcPPCode,
          underlyingPricingParams: [
            {
              underlyingID: form.data.instrumentId,
              dividend: proxy.lsh.assign(calcPriceParam.dividend,{'type':'2'}),
              volSurface: proxy.lsh.assign(calcPriceParam.volSurface,{'type':'2'}),
              funding: proxy.lsh.assign(calcPriceParam.discount,{'type':'2'}),
              spotPrice: calcPriceParam.spotPrice == '0' ? '' : calcPriceParam.spotPrice,
              spotQuoteType: calcPriceParam.spotQuoteType
            }
          ],
          discount: calcPriceParam.discount,
          'customPricingParam': ""
        },
    underlyingMarkets: [
      {
        'underlyingID': form.data.instrumentId,
        'fixingPrices': [],
        'observerPrices': []
      }
    ]
  }
  let calcInstrumentForm = toRaw(form.data)
  let result = props.result
  let json = {
    otcOption: otcOption,
    calcInstrumentForm: calcInstrumentForm,
    calcPricingForm: calcPriceParam,
    calcResultForm: result
  }
  return json;
}

/**
 * 5.6  低
 *   是否年化 监听  行权收益率 收益金额   名义本金  计息基准
 * */
watch(() => [form.data.lowMoneynessOption], ([newValue], [oldValue]) => {
  if (paymentAmountLinkValid(newValue, form.data.notional, form.data.lowPaymentAmountPecentage, form.data.lowPaymentAmount, form.data.daycounter, form.data.term)) {
    return
  }
  if (newValue == '1') {
    if (isNotVoidArg(String(form.data.notional), String(form.data.daycounter), String(form.data.term))) {
      let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
      if (isNotVoid(String(form.data.lowPaymentAmountPecentage))) {
        if (isEqualNum(0, form.data.lowPaymentAmountPecentage)) {
          form.data.lowPaymentAmount = 0
          return
        } else {
          form.data.lowPaymentAmount = form.data.lowPaymentAmountPecentage / 100 * annualized * form.data.notional
          return
        }
      }
      if (isNotVoid(String(form.data.lowPaymentAmount))) {
        if (isEqualNum(0, form.data.lowPaymentAmount)) {
          form.data.lowPaymentAmountPecentage = 0
          return
        } else {
          form.data.lowPaymentAmountPecentage = form.data.lowPaymentAmount * 100 / (annualized * form.data.notional)
          return
        }
      }

    }
  } else {
    if (isNotVoidArg(String(form.data.notional), String(form.data.lowPaymentAmountPecentage))) {
      if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, form.data.notional)) {
        form.data.lowPaymentAmount = form.data.lowPaymentAmountPecentage / 100 * form.data.notional
        return
      } else {
        form.data.lowPaymentAmount = 0
        return
      }
    }
    if (isNotVoidArg(String(form.data.lowPaymentAmount), String(form.data.lowPaymentAmountPecentage))) {
      if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, form.data.notional)) {
        form.data.lowPaymentAmountPecentage = form.data.lowPaymentAmount * 100 / form.data.notional
        return
      } else {
        form.data.lowPaymentAmountPecentage = 0
        return
      }
    }
  }

})
/**
 * 获取 定价配置 和  标的价格
 * */
watch(() => [props.cfgId,form.data.instrumentId], ([newId, oldId]) => {
  getPricingCfg()
  setTimeout(() => {
    proxy.$refs.PricingParamRef?proxy.$refs.PricingParamRef.calcRateByCode():''
  }, 1000);
})

const onBlurInstrumentId = () => {
  setSpotPrice()
  getPricingCfg()
  setTimeout(() => {
    proxy.$refs.PricingParamRef?proxy.$refs.PricingParamRef.calcRateByCode():''
  }, 1000);
}
watch(() => props.valueDate, (newValue, oldValue) => {
  if (newValue != oldValue) {
    setSpotPrice()
    //触发修改定价参数的价格
    proxy.$refs.PricingParamRef.setSpotPrice({
      valueDate:props.valueDate,
      instrumentId:form.data.instrumentId
    })
   proxy.$refs.PricingParamRef?proxy.$refs.PricingParamRef.calcRateByCode():''
  }
})
const onBlurEffectiveDate = () => {
  form.initFlag = false
  setSpotPrice()
}

watch(() => form.data.exerciseType, (nv, ov) => {
  if (form.data.exerciseType == 'American') {
    form.data.startDate = form.data.effectiveDate
    form.data.endDate = form.data.terminationDate
  }
}, {
  immediate: true
})
const props = defineProps({
  /*是否为重放*/
  isReCalc: {
    type: Boolean,
    default: false
  },
  calcSuccess:{
    type: String,
    default: ''
  },
  errorMsg:{
    type: String,
    default: ''
  },
  useType: {
    type: String,
    default: 'calc'
  },
  index: {
    type: String,
    default: ''
  },
  del: {
    type: Function,
    default: null
  },
  copy: {
    type: Function,
    default: null
  },
  calc: {
    type: Function,
    default: null
  },
  save2Trade: {type: Function, default: null},
  save2CalcLog: {type: Function, default: null},
  getDefaultCfg: {
    type: Function,
    default: null
  },
  calcPricingParam: {
    type: Object,
    default: {}
  },
  result: {
    type: Object,
    default: {}
  },
  setformData: {
    type: Function,
    default: null
  },
  formData: {
    type: Object,
    default: null
  },
  cfgId: {type: String, default: ''},
  valueDate: {type: String, default: ''}
})
const getResult = () => {
  var data = []
  for (var item in props.result) {
    if (['npv', 'delta', 'gamma', 'vega', 'theta', 'rho', 'spotPrice', 'dscRate'].includes(item)) {
      data.push({
        key: item,
        val: proxy.lsh.ceil(props.result[item], 4)
      })
    }
  }
  return data
}
//定价参数
const getPricingCfg = async () => {
  let query = {
    'calcPricingParam': {
      'cfgId': props.cfgId
    },
    'instrument': {
      'productType': productType,
      'termsheet': {
        'underlyerIds': [form.data.instrumentId],
        'customLegs': [{
          'legId': 'leg01',
          'productType': productType
        }
        ],
        'legs': [{
          'legId': 'leg01',
          'payDirection': '1'
        }
        ]
      }
    },
  }
  //设置定价参数中的定盘价
  proxy.$refs.PricingParamRef.setSpotPrice({
    valueDate:props.valueDate,
    instrumentId:form.data.instrumentId
  })
  if (props.getDefaultCfg != null) {
    props.getDefaultCfg(query, props.index)
  }
}
const cancel = () => {
  props.del(props.index)
}
const copy = () => {
  props.copy(props.index)
  cpoyWait.value = true;
  setTimeout(() => {
    cpoyWait.value = false;
  }, 1000);
}
const calc = (flag) => {
  /*flag 标识 为All时单次计算不会提示成功或失败*/
  getInstrumentJson().then((res) => {
    if (Boolean(res)) {
      let otcOption = res.otcOption
      otcOption.calcInstrumentForm = res.calcInstrumentForm
      otcOption.calcPricingForm = res.calcPricingForm
      if (otcOption != '') {
        props.calc(otcOption, props.index, flag)
      }
    }
  })
}
const getInstrumentJson = async () => {
  return new Promise((resolve) => {
    // if (Number(form.data.paymentAmountPecentage) <= Number(form.data.lowPaymentAmountPecentage)) {
    //   proxy.$modal.alertWarning('高档收益率必须大于低档收益率,请核对!')
    //   return
    // }
    // if (Number(form.data.paymentAmountPecentage) <= Number(form.data.midPaymentAmountPecentage)) {
    //   proxy.$modal.alertWarning('高档收益率必须大于中档收益率,请核对!')
    //   return
    // }
    // if (Number(form.data.midPaymentAmountPecentage) <= Number(form.data.lowPaymentAmountPecentage)) {
    //   proxy.$modal.alertWarning('中档收益率必须大于低档收益率,请核对!')
    //   return
    // }
    if(
        (Number(form.data.strikePercentage) ==0 && Number(form.data.lowStrike) ==0 )
        ||
        (Number(form.data.strike) ==0 && Number(form.data.lowStrikePercentage) ==0 )

    ){
      proxy.$modal.alertWarning('期初价格待定情况下,输入高行权价和低行权价时。请仅使用比例或仅使用金额!')
      return
    }
    if ((Number(form.data.strikePercentage) <= Number(form.data.lowStrikePercentage))
        &&(Number(form.data.strike) <= Number(form.data.lowStrike))) {
      proxy.$modal.alertWarning('高行权价必须大于低行权价,请核对!')
      return
    }
    proxy.$refs['formRef'].validate(valid => {
      if (valid) {
        validateDates(form)
        resolve(convertJson());
      } else {
        resolve('')
      }

    })
  })
}

/**
 *保存 交易
 * */
const save2Trade = () => {
  getInstrumentJson().then((res) => {
    if (Boolean(res)) {
      props.save2Trade(res)
    }
  })

}
/**
 *保存 草稿
 * */
const save2CalcLog = () => {
  getInstrumentJson().then((res) => {
    if (Boolean(res)) props.save2CalcLog(res)
  })
}
/**
 * 获取定价参数
 * */
const getCalcPriceParam = () => {
  let calcPriceParam = proxy.$refs.PricingParamRef.getCalcPricingParam()
  return calcPriceParam
}

const getFormData = () => {
  return form
}
/**
 *获取价格 价格类型
 * */
const setSpotPrice = () => {
  setCommonSpotPrice(proxy,form,props)
}
defineExpose({
  calc, getPricingCfg, getCalcPriceParam, getFormData, getInstrumentJson,proxy
})
onMounted(() => {
  form.initFlag = props.isReCalc
  if (props.formData != null) {
    let data = props.formData.data || props.formData
    form.data = Object.assign(form.data, data)
    form.initFlag = isNotVoid(props.calcPricingParam)

  }
  // 这里获取DOM的value是旧值
  nextTick(()=>{
    form.initFlag = false
  })
})
watch(() => proxy.$attrs.disabled, (nv) => {
  form.data.readOnly = nv
}, {
  immediate: true
})
watch(()=>form,()=>{

  proxy.$attrs.cacheFormData()
},{deep:true})
</script>
<style>

</style>

